import React, { PropTypes } from 'react';
import { Router, Route, IndexRoute, routerRedux } from 'dva/router';

import Layout from './routes/Layout';
import NotFoundPage from './routes/NotFound';

//////////////////// 登录注册
import { SigninPage, SignupMobilePage, SignupPasswordPage, SignupUserInfoPage, ForgotPasswordPage, ResetPasswordPage, SignAuthPage } from './routes/Account';

import { DepartPage } from './routes/Department';

//////////////////// 我的国医
import DoctorPage, { ConversationPage} from './routes/Doctor';
import { RequestListPage } from './routes/Doctor';

//////////////////// 健康管理
import HealthyPage from './routes/Healthy';

// 四诊采集
import Diagnosis from './routes/Healthy/Diagnosis';

// 四诊采集 - 智能舌镜
import { TongueListPage, TongueDetailPage, TongueReady, TongueSavlePage} from './routes/Healthy/Tongue';

// 四诊采集 - 测评量表
import {
  ScaleListPage,
  ScaleTemplate,
  ScaleMemberPage,
  ScaleRecordListPage,
  ScaleRecordDetailPage,
  // shiyan,
  //<Michael>
  ScalePhyTemplate,
  ScalePreTemplate,
  ScaleHypTemplate,
  ScaleHypDetailTemplate,
  ScaleHypBzfxTemplate,
  ScaleChdTemplate,
  ScaleTcmTemplate,
  ScaleDiaTemplate,
  ScalePartumTemplate,
  ScalePrePreTemplate,


  ScalePhySheet,
  ScalePreSheet,
  ScaleHypSheet,
  ScaleChdSheet,
  ScaleTcmSheet,
  ScaleDiaSheet,
  ScalePartumSheet,
  ScalePrePreSheet,

} from './routes/Healthy/Scale';

// 健康档案
import Archives from './routes/Healthy/Archives';

// 问诊单
import { InquiryListPage, InquiryDetailPage, InquirySheetPage } from './routes/Healthy/Inquiry';

// 体验报告
import { ReportListPage, ReportDetailPage } from './routes/Healthy/Report';

// 养生学堂
import { ArticleListPage, ArticleDetailPage } from './routes/Healthy/Article';

// 健康可餐
import { FoodListPage, FoodDetailPage } from './routes/Healthy/Food';

//////////////////// 我
import SettingsPage from './routes/Settings';

// 我 - 二维码
import QrcodePage from './routes/Settings/QrcodePage';

// 我 - 个人资料
import ProfilePage, { UpdateProfilePage, ValidateMobilePage, UpdateMobilePage, UpdatePasswordPage } from './routes/Settings/Profile';

// 我 - 家庭组
import { FamilyListPage, FamilyMemberPage } from './routes/Settings/Family';

// APP 端
import {
  NativeScalePage,
  NativeScaleReportPage,
  NativeScaleProgramePage,
  // <Michael>
  NativeScalePhyPage,
  NativeScalePhyReportPage,
  NativeScalePrePage,
  NativeScalePreReportPage,
  NativeScalePartumPage,
  NativeScalePartumReportPage,
  NativeScaleHypPage,
  NativeScaleHypBzfxPage,
  NativeScaleHypReportPage,
  NativeScaleChdPage,
  NativeScaleChdReportPage,
} from './routes/Native';

const requireAuth = (/* nextState, replace, next */ { dispatch, ...rest }) => {
  const state = rest.getState();
  if (state.routing.locationBeforeTransitions.pathname.indexOf('tongue') != -1) {

  }
  const auth = JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid")));
  // dispatch({type:"account/singauth",payload:{user:localStorage.getItem("gyhluser"+sessionStorage.getItem("hid")),pass:localStorage.getItem("gyhlpass"+sessionStorage.getItem("hid"))}})
  if (!auth) {
    dispatch(routerRedux.push('/signin'));
    location.reload();
  }
  // 判断，当网络问题或者是其他问题让tongue界面跳转的登录界面，然后在访问tongue。
  // 注： 当使用tongue功能时，调login接口（*singauth）
  if(auth){
    let url = location.href.split("#")[0];
    let hid = auth.hospitalId;
    dispatch({ type: 'wechat/init', payload: { url, hid  } });
  }
};

const nativeAuth = ({ dispatch, ...rest }) => {
  sessionStorage.setItem('token', 'app');
};

export default function ({ history, app }) {
  return (
    <Router history={history}>
      <Route path="/" component={Layout}>
        <IndexRoute component={HealthyPage} />
        <Route path="native" onEnter={() => nativeAuth(app._store)}>
          <Route path="scale" component={NativeScalePage} />
          <Route path="scale_report" component={NativeScaleReportPage} />
          <Route path="scale_programe" component={NativeScaleProgramePage} />
          <Route path="scale_phy" component={NativeScalePhyPage} />
          <Route path="scale_phy_report" component={NativeScalePhyReportPage} />
          <Route path="scale_pre" component={NativeScalePrePage} />
          <Route path="scale_pre_report" component={NativeScalePreReportPage} />
          <Route path="scale_hyp" component={NativeScaleHypPage} />
          <Route path="scale_hyp_bzfx" component={NativeScaleHypBzfxPage} />
          <Route path="scale_hyp_report" component={NativeScaleHypReportPage} />
          <Route path="scale_chd" component={NativeScaleChdPage} />
          <Route path="scale_chd_report" component={NativeScaleChdReportPage} />
          <Route path="scale_partum" component={NativeScalePartumPage} />
          <Route path="scale_partum_report" component={NativeScalePartumReportPage} />
        </Route>
          <Route path="department" component={DepartPage} />
          <Route path="signin" component={SigninPage} />
          <Route path="signup">
            <IndexRoute component={SignupMobilePage} />
            <Route path="2" component={SignupPasswordPage} />
            <Route path="3" component={SignupUserInfoPage} />
          </Route>
          <Route path="forgot">
            <IndexRoute component={ForgotPasswordPage} />
            <Route path="reset" component={ResetPasswordPage} />
          </Route>
          <Route path="signauth" component={SignAuthPage} />
          <Route path="doctor" onEnter={() => requireAuth(app._store)}>
            <IndexRoute component={DoctorPage} />
            <Route path=":id" component={ConversationPage} />
          </Route>
           <Route path="newdoctor">
            <IndexRoute component={RequestListPage} />
          </Route>
          <Route path="healthy" onEnter={() => requireAuth(app._store)}>
          <IndexRoute component={HealthyPage} />
          <Route path="scale_record">
            <IndexRoute component={ScaleRecordListPage} />
            <Route path=":id">
              <IndexRoute component={ScaleRecordDetailPage} />
              <Route path="sheet">
                <IndexRoute component={ScaleTemplate} />
                <Route path="phy" component={ScalePhySheet} />
                <Route path="phy2" component={ScalePhySheet} />
                <Route path="pre" component={ScalePreSheet} />
                <Route path="hyp" component={ScaleHypSheet} />
                <Route path="chd" component={ScaleChdSheet} />
                <Route path="tcm" component={ScaleTcmSheet} />
                <Route path="dia" component={ScaleDiaSheet} />
                <Route path="partum" component={ScalePartumSheet} />
                <Route path="prepre" component={ScalePrePreSheet} />
                <Route path="normal" component={ScalePrePreSheet} />
              </Route>
            </Route>
          </Route>
          <Route path="diagnosis">
            <IndexRoute component={Diagnosis} />
            <Route path="family">
              <IndexRoute component={ScaleMemberPage} />
                <Route path=":id">
                  <IndexRoute component={TongueReady} />
                </Route>
            </Route>
            <Route path="scale">
              <IndexRoute component={ScaleListPage} />
              {/*<Route path="phy" component={ScalePhyTemplate} />*/}
              <Route path="family">
                <IndexRoute component={ScaleMemberPage} />
                {/*<Route path=":id" component={ScaleTemplate} />*/}
                <Route path=":id">
                  <IndexRoute component={ScaleTemplate} />
                  <Route path="phy" component={ScalePhyTemplate} />
                  <Route path="phy2" component={ScalePhyTemplate} />
                  <Route path="pre" component={ScalePreTemplate} />
                  <Route path="hyp">
                    <IndexRoute component={ScaleHypTemplate} />
                    <Route path="detail" component={ScaleHypDetailTemplate} />
                    <Route path="bzfx" component={ScaleHypBzfxTemplate} />
                  </Route>
                  <Route path="chd" component={ScaleChdTemplate} />
                  <Route path="tcm" component={ScaleTcmTemplate} />
                  <Route path="dia" component={ScaleDiaTemplate} />
                  <Route path="partum" component={ScalePartumTemplate} />
                  <Route path="prepre" component={ScalePrePreTemplate} />
                  <Route path="normal" component={ScalePrePreTemplate} />
                </Route>
              </Route>
            </Route>
          </Route>
          <Route path="articles">
            <IndexRoute component={ArticleListPage} />
            <Route path=":id" component={ArticleDetailPage} />
          </Route>
          <Route path="archives">
            <IndexRoute component={Archives} />
            <Route path="save">
              <IndexRoute component={TongueSavlePage} />
            </Route>
            <Route path="tongue">
              <IndexRoute component={TongueListPage} />
              <Route path=":id" component={TongueDetailPage} />
            </Route>
            <Route path="inquiry">
              <IndexRoute component={InquiryListPage} />
              <Route path="sheet" component={InquirySheetPage} />
              <Route path=":id">
                <IndexRoute component={InquiryDetailPage} />
                <Route path="sheet" component={InquirySheetPage} />
              </Route>
            </Route>
            <Route path="report">
              <IndexRoute component={ReportListPage} />
              <Route path=":id">
                <IndexRoute component={ReportDetailPage} />
                <Route path="sheet">
                  <IndexRoute component={ScaleTemplate} />
                  <Route path="phy" component={ScalePhySheet} />
                  <Route path="phy2" component={ScalePhySheet} />
                  <Route path="pre" component={ScalePreSheet} />
                  <Route path="hyp" component={ScaleHypSheet} />
                  <Route path="chd" component={ScaleChdSheet} />
                  <Route path="tcm" component={ScaleTcmSheet} />
                  <Route path="dia" component={ScaleDiaSheet} />
                  <Route path="partum" component={ScalePartumSheet} />
                  <Route path="prepre" component={ScalePrePreSheet} />
                  <Route path="normal" component={ScalePrePreSheet} />
                </Route>
              </Route>
            </Route>
          </Route>
          <Route path="foods">
            <IndexRoute component={FoodListPage} />
            <Route path=":id" component={FoodDetailPage} />
          </Route>
        </Route>
        <Route path="settings" onEnter={() => requireAuth(app._store)}>
          <IndexRoute component={SettingsPage} />
          <Route path="qrcode" component={QrcodePage} />
          <Route path="update">
            <IndexRoute component={ProfilePage} />
            <Route path="profile" component={UpdateProfilePage} />
            <Route path="mobile">
              <IndexRoute component={ValidateMobilePage} />
              <Route path="2" component={UpdateMobilePage} />
            </Route>
            <Route path="password" component={UpdatePasswordPage} />
          </Route>
          <Route path="family">
            <IndexRoute component={FamilyListPage} />
            <Route path="add" component={FamilyMemberPage} />
            <Route path="update" component={FamilyMemberPage} />
          </Route>
        </Route>
      </Route>
      <Route path="*" component={NotFoundPage} />
    </Router>
  );
}
